package com.study.javaOther.Main;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.io.File;
import java.nio.charset.Charset;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName: MainDemo
 * @Description:
 * @author: 王胜
 * @date 2021/6/25
 * @Version: 1.0
 **/
public class MainDemo {
//    public static void main(String[] args) {
//        int threadCount = 10;
//        int total = JdbcUtil.getRowsCount("inf_organization");
//        int size = total / threadCount;
//
//        final CountDownLatch latch = new CountDownLatch(threadCount);
//        for (int i = 0; i < threadCount; i++) {
//            String sql = "SELECT * FROM inf_organization ORDER BY id LIMIT ?,?";
//            List<Organization> organizations = JdbcUtil.selectByPage(sql, i, size);
//            new Thread(new UpdateWorker(latch, organizations)).start();
//        }
//        try {
//            latch.await();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//
//    }
private static String filePath="C:\\Users\\yn\\Desktop\\工时.json";
private static String numFilePath="C:\\Users\\yn\\Desktop\\有效任务号.txt";

    public static void main(String[] args) {
        JSONArray json = JSONUtil.readJSONArray(new File(filePath), Charset.defaultCharset());
        List<String> txt = FileUtil.readLines(new File(numFilePath), Charset.defaultCharset());
        List<Pair<String,String>> txtContent;
        if (CollectionUtil.isNotEmpty(txt)) {
            txtContent=txt.stream().filter(StrUtil::isNotBlank).map(line->{
                String[] strings = line.split("\t");
                return Pair.of(strings[0].trim(),strings[1].trim());
            }).distinct().collect(Collectors.toList());
        }else{
            txtContent= Collections.emptyList();
        }
        Long totalNum=0L;
        Long total=0L;
        Long totalPlaned=0L;
        System.out.println(String.format("%-4s\t%-16s\t%-8s\t%-8s\t%-8s","序号","任务号","实际工时","计划工时","统计工时"));
        for (int i = 0; i < json.size(); i++) {
            JSONObject jsonObject = json.getJSONObject(i);
            JSONArray customFields = jsonObject.getJSONArray("customFields");
            String serialNumber="";
            Long sumActualLaborHour=0L;
            Long sumPlanedLaborHour=0L;
            for (int i1 = 0; i1 < customFields.size(); i1++) {
                JSONObject jsonObject1 = customFields.getJSONObject(i1);
                String fieldIdentifier = jsonObject1.getStr("fieldIdentifier");
                if(sumActualLaborHour==0L && ("sumActualLaborHour".equals(fieldIdentifier)||"101587".equals(fieldIdentifier))){
                    sumActualLaborHour = jsonObject1.getLong("value");
                }
                if(sumPlanedLaborHour==0L && ("sumPlanedLaborHour".equals(fieldIdentifier)||"101586".equals(fieldIdentifier))){
                    sumPlanedLaborHour = jsonObject1.getLong("value");
                }
                if("serialNumber".equals(fieldIdentifier)){
                    serialNumber=jsonObject1.getStr("value");
                }
            }
            String finalSerialNumber = serialNumber;
            Optional<Pair<String, String>> first = txtContent
                    .stream()
                    .filter(item ->Objects.equals(item.getKey(), finalSerialNumber))
                    .findFirst();
            if(!first.isPresent()){
                continue;
            }
            totalPlaned+= sumPlanedLaborHour;
            total+= sumActualLaborHour;
            totalNum++;
            System.out.println(String.format(
                    "%-4s\t" +
                            "%-16s\t" +
                            "%-8s\t" +
                            "%-8s\t" +
                            "%-8s\t" +
                            "%-4s",
                    totalNum,serialNumber,sumActualLaborHour,sumPlanedLaborHour,first.get().getValue(),Objects.equals(sumActualLaborHour+"",first.get().getValue())?"√":"×"));
        }
        System.out.println("总条数："+totalNum+"\t总实际工时："+total+"\t总预计工时："+totalPlaned);
    }
}
